package ru.marketsoft.reminder;

import ru.marketsoft.reminder.frames.*;
import ru.marketsoft.reminder.threads.Dump;
import ru.marketsoft.reminder.threads.HideInfoTime;
import ru.marketsoft.reminder.threads.IVisible;
import ru.marketsoft.reminder.tools.Settings;
import ru.marketsoft.reminder.tools.Utils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

/**
 * User: dimka3210
 * Date: 28.02.13
 * Time: 20:38
 */
public class Main {
    private static MenuItem itemSound1;
    private static MenuItem itemSound2;
    public static void main(String args[]) throws AWTException, IOException {
        String onString;
        String offString;
        IFrame frame = new IFrame();
        Thread ivisible = new Thread(new IVisible(frame));
        ivisible.start();

        try {
            SystemTray tray = SystemTray.getSystemTray();
            TrayIcon trayIcon = Dump.getTrayIcon();
            trayIcon.setImageAutoSize(true);
            tray.add(trayIcon);

            PopupMenu popupMenu = new PopupMenu();
            MenuItem item1 = new MenuItem("Установить время");
            MenuItem item2 = new MenuItem("Остановить напоминалку");
            MenuItem item3 = new MenuItem("Настройки");

            PopupMenu popupMenu1 = new PopupMenu("Звук");

            if (Settings.getInstance().getProperty("sound").equals("off")) {
                onString = "Вкл.";
                offString = Utils.getChecked() + "Выкл.";
            } else {
                onString = Utils.getChecked() + "Вкл.";
                offString = "Выкл.";
            }


            itemSound1 = new MenuItem(onString);
            itemSound2 = new MenuItem(offString);
            popupMenu1.add(itemSound1);
            popupMenu1.add(itemSound2);

            popupMenu.add(item1);
            popupMenu.addSeparator();
            popupMenu.add(item2);
            popupMenu.addSeparator();
            popupMenu.add(item3);
            popupMenu.add(popupMenu1);

            trayIcon.setPopupMenu(popupMenu);
            trayIcon.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JFrame infoTime = InfoTime.getInstance();
                    InfoTime.getInstance().Repaint();
                    infoTime.setVisible(true);

                    new Thread(new HideInfoTime(infoTime)).start();
                }
            });


            item1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JDialog setTime = SetTime.getInstance();
                    setTime.setVisible(true);
                }
            });
            item2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(WindowConstants.DISPOSE_ON_CLOSE);
                }
            });
            item3.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    SettingsFrame settingsFrame = SettingsFrame.getInstance();
                    settingsFrame.setVisible(true);
                }
            });

            itemSound1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Settings.getInstance().setProperty("sound", "on");
                    itemSound1.setLabel(Utils.getChecked() + "Вкл.");
                    itemSound2.setLabel("Выкл");
                }
            });

            itemSound2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Settings.getInstance().setProperty("sound", "off");
                    itemSound2.setLabel(Utils.getChecked() + "Выкл.");
                    itemSound1.setLabel("Вкл");
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
            new ErrorDialog("Ваша ОС не поддерживает трей.");
        }
    }
}
